package cn.px.rest.controller.repair;

import cn.px.common.bean.AbstractPageResultBean;
import cn.px.common.util.BeanConverterUtil;
import cn.px.common.util.R;
import cn.px.common.validation.group.AddGroup;
import cn.px.common.validation.group.UpdateGroup;
import cn.px.dao.dto.repair.RepairPartUsedDTO;
import cn.px.dao.entity.repair.RepairPartUsed;
import cn.px.dao.util.ExcelUtil;
import cn.px.dao.vo.export.repair.ExportRepairPartUsedVO;
import cn.px.dao.vo.query.repair.RepairPartUsedVO;
import cn.px.rest.log.annotation.SysLog;
import cn.px.service.repair.RepairPartUsedService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description 维修更换备件 控制器
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2020,北京乾元坤和科技有限公司 All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("/repair/part-useds")
public class RepairPartUsedController {

    private final RepairPartUsedService repairPartUsedService;

    /**
     * 通过id查询单条记录
     *
     * @param id 主键
     * @return R
     */
    @GetMapping("/{id}")
    public R<RepairPartUsed> getById(@PathVariable("id") String id) {
        return new R<>(repairPartUsedService.getModelById(id));
    }

    /**
     * 新增记录
     *
     * @param repairPartUsedDTO 维修更换备件DTO
     * @return R
     */
    @SysLog("新增维修更换备件")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('repair-part-useds-add')")
    public R save(@Validated({AddGroup.class}) @RequestBody RepairPartUsedDTO repairPartUsedDTO) {
        return new R<>(repairPartUsedService.saveModelByDTO(repairPartUsedDTO));
    }

    /**
     * 修改记录
     *
     * @param repairPartUsedDTO 维修更换备件DTO
     * @return R
     */
    @SysLog("修改维修更换备件")
    @PutMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('repair-part-useds-edit')")
    public R update(@PathVariable("id") String id, @Validated({UpdateGroup.class}) @RequestBody RepairPartUsedDTO repairPartUsedDTO) {
        repairPartUsedService.modModelByDTO(repairPartUsedDTO);
        return new R<>();
    }

    /**
     * 通过id删除一条记录
     *
     * @param id 主键
     * @return R
     */
    @SysLog("删除维修更换备件")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('repair-part-useds-del')")
    public R removeById(@PathVariable String id) {
        repairPartUsedService.deleteByPrimaryKey(id);
        return new R<>();
    }

    /**
     * 批量记录
     *
     * @param ids 主键
     * @return R
     */
    @SysLog("批量删除维修更换备件")
    @DeleteMapping("")
    @PreAuthorize("@pms.hasPermission('repair-part-useds-del')")
    public R removeIds(@RequestBody List<String> ids) {
        repairPartUsedService.batchDelete(ids);
        return new R<>();
    }

    /**
     * 获取分页
     *
     * @param pageNum           当前页码
     * @param pageSize          每页条数
     * @param repairPartUsedDTO 维修更换备件DTO
     * @return R
     */
    @GetMapping("/page")
    public R<AbstractPageResultBean<RepairPartUsedVO>> query(RepairPartUsedDTO repairPartUsedDTO, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "20") int pageSize) {
        return new R<>(repairPartUsedService.selectPageList(repairPartUsedDTO, pageNum, pageSize));
    }

    /**
     * 获取列表
     *
     * @param repairPartUsedDTO 维修更换备件DTO
     * @return R
     */
    @GetMapping("")
    public R query(RepairPartUsedDTO repairPartUsedDTO) {
        return new R<>(repairPartUsedService.getModelListByDTO(repairPartUsedDTO));
    }

    /**
     * 维修更换备件导出
     *
     * @param repairPartUsedDTO 维修更换备件DTO
     * @return R
     */
    @GetMapping("/export")
    @SysLog("维修更换备件导出")
    @PreAuthorize("@pms.hasPermission('repair-part-useds-export')")
    public void export(HttpServletResponse response, RepairPartUsedDTO repairPartUsedDTO) {
        List<RepairPartUsed> list = repairPartUsedService.getModelListByDTO(repairPartUsedDTO);
        ExcelUtil.exportResponseDict(response, ExportRepairPartUsedVO.class, BeanConverterUtil.copyListProperties(list, ExportRepairPartUsedVO.class), "维修更换备件");
    }

}
